package org.rcgwt.client.ui.grid.internal;

import org.rcgwt.client.ui.grid.internal.model.ICellModel;
import org.rcgwt.client.ui.grid.internal.model.IColumnModel;
import org.rcgwt.client.ui.grid.internal.model.IElementModel;
import org.rcgwt.client.ui.grid.internal.model.IGroupModel;
import org.rcgwt.client.ui.grid.internal.model.IRowModel;

/**
 * 
 * @author Olivier Oeuillot
 */
public class RowModel extends AbstractElementModel implements IRowModel {

    protected static final RowModel sharedRowModel = new RowModel();

    private static final CellModel sharedCellModel = new CellModel();

    protected IGroupModel groupModel;

    private RowModel() {
    }

    protected RowModel(int modelIndex, int stateMask, IGroupModel groupModel) {
        set(modelIndex, stateMask, groupModel);

        groupModel.getGridModel().registerRowModel(this);
    }

    @Override
    public Type getType() {
        return Type.ROW;
    }

    protected void set(int modelIndex, int stateMask, IGroupModel group) {
        assert group != null : "Group is NULL !";

        this.groupModel = group;

        set(modelIndex, stateMask);
    }

    @Override
    public IGroupModel getGroupModel() {
        return groupModel;
    }

    @Override
    public IElementModel getParent() {
        return getGroupModel();
    }

    @Override
    public String toString() {
        return "[Row modelIndex=" + modelIndex + " state=0x"
                + Integer.toHexString(stateMask) + " group='" + getGroupModel()
                + "']";
    }

    @Override
    protected void constructKey(IKey key) {
        super.constructKey(key);

        groupModel.appendKey(key);
    }

    @Override
    public ICellModel setupSharedCellModel(IColumnModel columnModel) {
        sharedCellModel.set(0, this, columnModel);

        return sharedCellModel;
    }

    @Override
    public ICellModel getCellModel(IColumnModel columnModel) {
        CellModel cellModel = new CellModel();

        cellModel.set(0, this, columnModel);

        return cellModel;
    }

}
